var f_ready = function () {
    var isReady = false,
        //函数列表
        readyList = [],
        //触发事件函数
        _fire,
        doc=document,win=window,f=null;

    var ftype = function (o, type) {
        var _t, curtype;
        curtype = ((_t = typeof(o)) == "object" ?
            o == null && "null" || Object.prototype.toString.call(o).slice(8, -1) :
            _t
            ).toLowerCase();
        if (type) {
            return type === curtype;
        } else {
            return curtype;
        }
    };
    if ('addEventListener' in doc) {
        //w3c
        _fire = function () {
            doc.removeEventListener('DOMContentLoaded', _fire, false);
            _fireReady();
        };
        doc.addEventListener('DOMContentLoaded', _fire, false);
    } else {
        //  <= ie8
        _fire = function () {
            doc.detachEvent('onreadystatechange', _fire);
            if (doc.readyState == 'complete') {
                _fireReady();
            }
        };
        doc.attachEvent('onreadystatechange', _fire);
        (function () {
            try {
                //doScroll 如果抛出异常，说明dom未就绪
                doc.documentElement.doScroll('left');
                _fireReady();
            } catch (e) {
                setTimeout(arguments.callee, 10);
            }
        })();

        //win.attachEvent('onload',_fireReady);
    }
    /**
     * 触发所有绑定函数
     */
    function _fireReady() {
        if (isReady) {
            return;
        }
        isReady = true;
        // 如果有绑定的函数
        if (readyList.length > 0) {
            //执行全部绑定的
            var fn, i = 0;
            while (fn = readyList[i++]) {
                fn.call(win, f);
            }
            //重置
            readyList = [];
        }
    }

    return function (fn) {
        if (!ftype(fn, 'function')) {
            return false;
        }
        if (isReady) {
            // Execute the function immediately
            fn.call(win, f);
        } else {
            // Remember the function for later
            readyList.push(fn);
        }
    };
}();
 (function () {

    var DelayLoad={
        odelayload:document,
        needele:null,
        loadsrc:'',
        getneedele:function(){
            var eleobj={img:[],src:[]},
                allimg= this.odelayload.getElementsByTagName('img');
            for(var i=0;i<allimg.length;i++){
                eleobj.img.push(allimg[i]);
               // eleobj.mask.push(this.getclass(allimg[i].parentNode.getElementsByTagName('div'),'mask'));

            }
            return eleobj;

        },
        getclass:function(eles,cname){
            for(var i=0;i<eles.length;i++){
                if(eles[i].className===cname){
                    return eles[i];
                }
            }
        },

        offset:function (ele ) {
            var oRect = ele.getBoundingClientRect(),
                sct=document.body.scrollTop,
                docl =0,
                doct = 0,
                offset;


            if (typeof arguments.callee.offset != "number") {
                var temp = document.createElement("div");
                temp.style.cssText = "position:absolute;left:0;top:0;";
                document.body.appendChild(temp);
                arguments.callee.offset = -temp.getBoundingClientRect().top - sct;
                document.body.removeChild(temp);
                temp = null;
            }
            offset = arguments.callee.offset;
            return oRect.top + offset + doct;
        },
        showimg:function(num){
            var _this=this,curimg,cursrc;
            for(var i=0;i<num;i++){
                curimg=this.needele.img.shift();
                cursrc=this.needele.src.shift();
               // curmask=this.needele.mask.shift();
             //   (function(curmask){
                    curimg.onload=function(){
                     //   curmask.parentNode.removeChild(curmask);
                        this.style.opacity=0;
                        this.style.filter='alpha(opacity=100)';
                        _this.fadein(this);

                    };
            //    })(curmask);

                curimg.src=cursrc;

            }
        },
        fadein:function(ele){

            var times=1000/100,i=0;
            var t=setInterval(function(){
                if(i===times){
                    clearInterval(t);
                }
                ele.style.opacity=i/10;
                ele.style.filter='alpha(opacity='+i*10+')';
                i+=2;
            },100);

        },
        isshow:function(ele){
            var doc=document,docElemProp;
                docElemProp = doc.documentElement['clientHeight'];

             var winh= doc.compatMode === 'CSS1Compat' && docElemProp ||  doc.body['clientHeight'] || docElemProp;

                return ele&&this.offset(ele)<winh;
        },
        getshownum:function(){
            var num=0,needimg=this.needele.img;
            for(var i=0;i<needimg.length;i++){
                if(this.isshow(needimg[i])){
                    num++;
                }
            }
            return num;
        },
        readyimg:function(){
            var needimg=this.needele.img;
            for(var i=0;i<needimg.length;i++){

                this.needele.src.push(needimg[i].src);
                needimg[i].src=this.loadsrc;

            }
        },
        onscroll:function(){
            var _this=this;
            window.onscroll=function(){
                _this.showimg(_this.getshownum());
             }
        },
        init:function(loading){
            this.loadsrc=loading;
            this.needele=this.getneedele();
            this.readyimg();
            this.showimg(this.getshownum());
            this.onscroll();
        }
    };
    f_ready(function(){

        DelayLoad.init('loading.gif');
    });
})();
